package com.springmvc.controller;

import com.springmvc.pojo.TUsers;
import com.springmvc.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 咕唧
 * @Date 2022/5/22 13:29
 * @Version 1.0
 */
//@Controller
@RestController
@RequestMapping("/user")
//@ResponseBody//Controller类上加@ResponseBody注解,等价于在类中的每个方法上都加了@ResponseBody
public class JsonController {

    private TUserService tUserService;

    @Autowired
    public void settUserService(TUserService tUserService) {
        this.tUserService = tUserService;
    }

    @RequestMapping("/find")
    //  @ResponseBody
    public TUsers findByName(String name) {
        TUsers users = tUserService.findByName(name);
        System.out.println(users);
        return users;
    }

    //produces 防止中文乱码
    @RequestMapping(value = "/hello", produces = "text/html;charset=utf-8")
    //@ResponseBody
    public String hello() {
        //如果返回值是基本类型，则不需要转json，直接将字符串响应给客户端
        return "hello";
    }

    //@ResponseBody
    @PostMapping("/adduser")
    public TUsers addUsers(@RequestBody TUsers tUser){
        tUser.setName("name----"+tUser.getName());
        return tUser;
    }
}
